package com.ruoyi.quartz.controller;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.quartz.domain.SysJobInfoConfig;
import com.ruoyi.quartz.domain.SysJobLog;
import com.ruoyi.quartz.service.ISysJobInfoConfigService;
import com.ruoyi.quartz.service.ISysJobLogService;

/**
 * job日志信息Controller
 * 
 * @author ruoyi
 * @date 2023-05-02
 */
@Controller
@RequestMapping("/monitor/jobLog")
public class SysJobLogController extends BaseController
{
	private String prefix = "monitor/job";

	@Autowired
	private ISysJobLogService sysJobLogService;
	
    @Autowired
    private ISysJobInfoConfigService sysJobInfoConfigService;

	@RequiresPermissions("monitor:job:view")
	@GetMapping()
	public String log(@RequestParam(value = "jobId", required = false) Long jobId, ModelMap mmap)
	{
		if (StringUtils.isNotNull(jobId))
		{
			SysJobInfoConfig job = sysJobInfoConfigService.selectSysJobInfoConfigById(jobId);
			mmap.put("job", job);
		}
		return prefix + "/jobLog";
	}

	/**
	 * 查询job日志信息列表
	 */
	@RequiresPermissions("monitor:job:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(SysJobLog sysJobLog)
	{
		startPage();
		List<SysJobLog> list = sysJobLogService.selectSysJobLogList(sysJobLog);
		return getDataTable(list);
	}

	/**
	 * 导出job日志信息列表
	 */
	@RequiresPermissions("monitor:job:export")
	@Log(title = "job日志信息", businessType = BusinessType.EXPORT)
	@PostMapping("/export")
	@ResponseBody
	public AjaxResult export(SysJobLog sysJobLog)
	{
		List<SysJobLog> list = sysJobLogService.selectSysJobLogList(sysJobLog);
		ExcelUtil<SysJobLog> util = new ExcelUtil<SysJobLog>(SysJobLog.class);
		return util.exportExcel(list, "job日志信息数据");
	}

	/**
	 * 新增job日志信息
	 */
	@GetMapping("/add")
	public String add()
	{
		return prefix + "/add";
	}

	/**
	 * 新增保存job日志信息
	 */
	@RequiresPermissions("monitor:job:add")
	@Log(title = "job日志信息", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(SysJobLog sysJobLog)
	{
		return toAjax(sysJobLogService.insertSysJobLog(sysJobLog));
	}

	/**
	 * 修改job日志信息
	 */
	@RequiresPermissions("monitor:job:edit")
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Long id, ModelMap mmap)
	{
		SysJobLog sysJobLog = sysJobLogService.selectSysJobLogById(id);
		mmap.put("sysJobLog", sysJobLog);
		return prefix + "/edit";
	}

	/**
	 * 修改保存job日志信息
	 */
	@RequiresPermissions("monitor:job:edit")
	@Log(title = "job日志信息", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(SysJobLog sysJobLog)
	{
		return toAjax(sysJobLogService.updateSysJobLog(sysJobLog));
	}

	/**
	 * 删除job日志信息
	 */
	@RequiresPermissions("monitor:job:remove")
	@Log(title = "job日志信息", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{
		return toAjax(sysJobLogService.deleteSysJobLogByIds(ids));
	}

	@RequiresPermissions("monitor:job:detail")
	@GetMapping("/detail/{id}")
	public String detail(@PathVariable("id") Long id, ModelMap mmap)
	{
		mmap.put("name", "jobLog");
		mmap.put("jobLog", sysJobLogService.selectSysJobLogById(id));
		return prefix + "/detail";
	}

	@Log(title = "调度日志", businessType = BusinessType.CLEAN)
	@RequiresPermissions("monitor:job:remove")
	@PostMapping("/clean")
	@ResponseBody
	public AjaxResult clean()
	{
		sysJobLogService.cleanJobLog();
		return success();
	}
}
